package  
{
	import org.flixel.FlxG;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import org.flixel.FlxObject;
	import org.flixel.FlxU;
	import org.flixel.plugin.photonstorm.FlxBar;
	import org.flixel.plugin.photonstorm.FlxMath;
	
	/**
	 * ...
	 * @author Enric Llagostera
	 */
	public class BlockyPlayer extends FlxSprite 
	{
		[Embed(source = "../assets/BlockPlayerTestYellow.png")] private var imgBlockyPlayerYellow:Class;
		[Embed(source = "../assets/BlockPlayerTestPurple.png")] private var imgBlockyPlayerPurple:Class;
		[Embed(source = "../assets/BlockHazard.png")] private var imgMine:Class;
		[Embed(source = "../assets/BlockWall.png")] private var imgWall:Class;
		[Embed(source = "../assets/BlockMult.png")] private var imgMult:Class;
		[Embed(source = "../assets/fullBlockUI.png")] public var imgBlockUIRed:Class;
		[Embed(source = "../assets/fullBlockUI2.png")] public var imgBlockUIBlue:Class;
		
		[Embed(source = "../assets/shieldPurple.png")] public var imgShieldPurple:Class;
		[Embed(source = "../assets/shieldYellow.png")] public var imgShieldYellow:Class;
		[Embed(source = "../assets/shieldPurpleVertical.png")] public var imgShieldPurpleVertical:Class;
		[Embed(source = "../assets/shieldYellowVertical.png")] public var imgShieldYellowVertical:Class;
		
		public var team:int = 0;
		
		private var moveAccel:Number;
		
		private var keyUp:String;
		private var keyDown:String;
		private var keyLeft:String;
		private var keyRight:String;
		private var keyWall:String;
		private var keyMine:String;
		private var keyMult:String;
		private var keyShield:String;
		
		public var numBlocks:int;
		public var maxBlocks:int;
		public var rateAddBlock:Number;
		public var lastpos:FlxPoint = new FlxPoint(0, 0);
		
		private var activeKey:String = null;
		private var activeBlock:String = "wall";
		
		public var placingBlock:String;
		
		private var tempBlock:FlxSprite;
		private var shieldBlock:FlxSprite;
		
		public var blockBar:FlxBar;
		
		private var canPlace:Boolean;
		
		private var verticalShield:Boolean = false;
		
		private var oldPosShield:FlxPoint = new FlxPoint();
		
		public function BlockyPlayer(X:Number, Y:Number, pTeam:int,
							keyUp:String, keyDown:String, keyLeft:String, keyRight:String,
							keyWall:String, keyMine:String, keyMult:String, keyShield:String,isPurple:Boolean) 
		{
			super(X, Y);
			lastpos.x = X;
			lastpos.y = Y;
			
			
			shieldBlock = new FlxSprite
			
			
			if (isPurple)
			{
				loadGraphic(imgBlockyPlayerPurple, false, true, 32, 32);
			}
			else
			{
				loadGraphic(imgBlockyPlayerYellow, false, true, 32, 32);
			}
			
			width = 28;
			offset.x = 2;
			
			ID = team = pTeam;
			
			drag.x = drag.y = 400;
			moveAccel 		= 400;
			maxVelocity.x = maxVelocity.y = 120;
			
			mass = 1;
			
			numBlocks = 5;
			maxBlocks = 5;
			
			placingBlock = "nothing";
			
			tempBlock = new FlxSprite(x, y);
			tempBlock.makeGraphic(36, 36, 0x66ffffff);
			Registry.groupTemp.add(tempBlock);
			
			tempBlock.visible = false;
			
			this.keyUp = keyUp;
			this.keyDown = keyDown;
			this.keyLeft = keyLeft;
			this.keyRight = keyRight;
			this.keyWall = keyWall;
			this.keyMine = keyMine;
			this.keyMult = keyMult;
			this.keyShield = keyShield;
			
			activeBlock = "wall";
			activeKey = keyWall;
			
			blockBar = new FlxBar(X, Y - 7, FlxBar.FILL_LEFT_TO_RIGHT, 30, 4, this, "numBlocks", 0, maxBlocks, false);
			
			if (team == 1)
				blockBar.createImageBar(null, imgBlockUIRed, 0x00000000, 0x00000000);
			else
				blockBar.createImageBar(null, imgBlockUIBlue, 0x00000000, 0x00000000);
			
			
			Registry.groupUI.add(blockBar);
			
			FlxG.watch(this, "x");
		}
		
		private function processMovement():void {
			
			if (FlxG.state is PlayState && (FlxG.state as PlayState).gameSTarte)
			{
				acceleration.x = acceleration.y = 0;
				
				if (placingBlock == "nothing" && activeBlock != "shield") {
					
					// movement
					if (FlxG.keys.pressed(keyUp)) {
						
						if (velocity.y > 0) velocity.y = 0;
						acceleration.y = -moveAccel;
					}
					
					if (FlxG.keys.pressed(keyRight)) {
						
						if (velocity.x < 0) velocity.x = 0;
						acceleration.x = moveAccel;
						
					}
					
					if (FlxG.keys.pressed(keyDown)) {
						
						if (velocity.y < 0) velocity.y = 0;
						acceleration.y = moveAccel;
						
					}
					
					if (FlxG.keys.pressed(keyLeft)) {
						
						if (velocity.x > 0) velocity.x = 0;
						acceleration.x = -moveAccel;
						
					}
					
				}
				
			}
			
			
			
		}
		
		private function processBlock():void {
			
			if (FlxG.keys.justPressed(keyWall) && !FlxG.keys.pressed(keyShield)) {
				activeKey = keyWall;
				activeBlock = "wall";
				
				tempBlock.loadGraphic(imgWall);
				
				tempBlock.width = tempBlock.height = 28;
				tempBlock.offset.x = tempBlock.offset.y = 2;
				
				tempBlock.alpha = 0.5;
			}
			else if (FlxG.keys.justPressed(keyMine) && !FlxG.keys.pressed(keyShield)) {
				activeKey = keyMine;
				activeBlock = "mine";
				
				tempBlock.loadGraphic(imgMine);
				
				tempBlock.width = 28;
				tempBlock.height = 16;
				tempBlock.offset.x = 2;
				tempBlock.offset.y = 14;
				
				tempBlock.alpha = 0.5;
			}
			else if (FlxG.keys.justPressed(keyMult) && !FlxG.keys.pressed(keyShield)) {
				activeKey = keyMult;
				activeBlock = "mult";
				
				tempBlock.loadGraphic(imgMult);
				tempBlock.width = tempBlock.height = 28;
				tempBlock.offset.x = tempBlock.offset.y = 2;
				
				tempBlock.alpha = 0.5;
			}
			// turning into shield
			else if (FlxG.keys.justPressed(keyShield)) {
				
				oldPosShield.x = x;
				oldPosShield.y = y;
				
				activeKey = keyShield;
				activeBlock = "shield";
				placingBlock = "nothing";
				
				velocity.y = velocity.x = 0;
			}
			
			// block placing preparation
			if (FlxG.keys.justPressed(activeKey)) {
				
				velocity.y = velocity.x = 0;
				
				if (numBlocks > 0 && activeBlock != "shield") {
					
					placingBlock = "placing";
					
				}
				
				
			}
			
			
			// when it is a shield
			if (activeBlock == "shield" && FlxG.keys.pressed(keyShield)) {
				
				
				if (FlxG.keys.pressed(keyDown) || FlxG.keys.pressed(keyUp)) {
					
					verticalShield = true;
					
					immovable = true;
					
					this.x = oldPosShield.x;
					this.y = oldPosShield.y - 16;
					
					this.height = 64;
					this.width = 32;
					
					if (team == 1) this.loadGraphic(imgShieldYellowVertical);
					else this.loadGraphic(imgShieldPurpleVertical);
				}
				else {
					
					verticalShield = false;
					
					immovable = true;
					
					this.width = 64;
					this.height = 32;
					
					this.x = oldPosShield.x - 16;
					this.y = oldPosShield.y;
					
					if (team == 1) this.loadGraphic(imgShieldYellow);
					else this.loadGraphic(imgShieldPurple);
				}
			}
			
			
			
			if (FlxG.keys.justReleased(keyShield)) {
				
				x = oldPosShield.x;
				y = oldPosShield.y;
				
				verticalShield = false;
				
				if (team == 1) this.loadGraphic(imgBlockyPlayerYellow);
				else this.loadGraphic(imgBlockyPlayerPurple);
				
				this.width = 28;
				this.height = 32;
				this.offset.x = 2;
				
				immovable = false;
				
				placingBlock = "nothing";
				activeBlock = "nothing";
			}
			
			// action of block placing
			if (FlxG.keys.pressed(activeKey) && placingBlock == "placing") {
				
				tempBlock.x = x;
				tempBlock.y = y;
				
				tempBlock.visible = true;
				
				if (FlxG.keys.pressed(keyUp)) {
					tempBlock.y -= height - tempBlock.offset.y + 10;
				}
				
				if (FlxG.keys.pressed(keyRight)) {
					tempBlock.x += width + tempBlock.offset.x + 8;
				}
				
				if (FlxG.keys.pressed(keyDown)) {
					tempBlock.y += height + tempBlock.offset.y/2 + 8;
				}
				
				if (FlxG.keys.pressed(keyLeft)) {
					tempBlock.x -= width - tempBlock.offset.x + 8;
				}
				
				canPlace = true;
				
				if (activeBlock == "mine") {
					
					if (FlxG.overlap(tempBlock, Registry.safeArea1)) {
						canPlace = false;
						Registry.safeArea1.visible = true;
						Registry.safeArea1.flicker(0.5);
					}
					
					if (FlxG.overlap(tempBlock, Registry.safeArea2)) {
						canPlace = false;
						Registry.safeArea2.visible = true;
						Registry.safeArea2.flicker(0.5);
					}
						
					if (!canPlace) {
						tempBlock.visible = false;
						//placingBlock = "nothing";
					}
				}
				
			}
			
			// relaese block placing
			if (FlxG.keys.justReleased(activeKey) && 
				placingBlock == "placing") {
				
				if ((FlxG.keys.pressed(keyUp) || FlxG.keys.pressed(keyRight)  || FlxG.keys.pressed(keyDown) || FlxG.keys.pressed(keyLeft)) &&
					canPlace) {
					
					// place block
					var aux:Block = new Block(tempBlock.x, tempBlock.y, activeBlock, this);
					
					var healthBar:FlxBar;
					
					if (activeBlock == "mult")
					{
						FlxG.play(Registry.placeprismsnd);
						healthBar = new FlxBar(tempBlock.x + 30, tempBlock.y - 4, FlxBar.FILL_BOTTOM_TO_TOP, 4, 30, aux, "health", 0, aux.health); 
					}
					else
					{
						if (activeBlock == "wall")
						{
							FlxG.play(Registry.placewallsnd);
						}
						else
						{
							FlxG.play(Registry.placespikesnd);
						}
						healthBar = new FlxBar(tempBlock.x + 30, tempBlock.y - 10, FlxBar.FILL_BOTTOM_TO_TOP, 4, 30, aux, "health", 0, aux.health); 
					}
						
					if (team == 1)
						healthBar.createFilledBar(0xff000000, 0xffffff00, true, 0xff000000);
					else
						healthBar.createFilledBar(0xff000000, 0xff6b3d99, true, 0xff000000);
					
					healthBar.immovable = true;
					healthBar.killOnEmpty = true;
					Registry.groupUI.add(healthBar);
					
					
					FlxObject.separate(this, aux);
					
					Registry.groupBlocks.add(aux);
					
					
					numBlocks--;
					
				}
				
				placingBlock = "nothing";
				tempBlock.visible = false;
				
				
			}
			
		}
		
		override public function update():void {
			
			/*FlxG.log(Math.abs(lastpos.x - x));
			
			if (Math.abs(lastpos.x - x) > 10) {
				x = lastpos.x;
			}
			
			if (Math.abs(lastpos.y - y) > 10) {
				y = lastpos.y;
			}5*/
			
			super.update();
			
			processBlock();
			processMovement();
			
			
			
			
			
			
			
			
			
			
			/*lastpos.x = x;
			lastpos.y = y;*/
			
		}
		
		
	}

}